package com.lagou.factory;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

import java.lang.reflect.Proxy;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @类名：com.lagou.proxy.ProxyFactory
 * @作者 mengwei
 * 创建日期：2020/10/15 11:12
 */
public class ProxyFactory {
    private ProxyFactory() {
    }

    private static ProxyFactory proxyFactory=new ProxyFactory();
    public static ProxyFactory newInstance() {
        return proxyFactory;
    }
    public <T> T getJdkProxy(T obj){

      return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), (proxy, method, args) -> {
          System.out.println("before");
          Object invoke = method.invoke(obj, args);
          System.out.println("after");
          return invoke;
      });

   }
    public <T> T getCglibProxy(T obj){

      return (T) Enhancer.create(obj.getClass(), (MethodInterceptor) (o, method, objects, methodProxy) -> {
          System.out.println("cglib-factory-before");
          Object invoke = method.invoke(obj, objects);
          System.out.println("cglib-factory-after");

          return invoke;
      });

   }



}
